package projectwork;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.openqa.selenium.By;


public class projectFunction extends BaseClass{


	/* Name of the Method: enterText
	 * Method Description: Entering text value to the text field
	 * Arguments: xpathobj --> Xpath of the object, textValue --> Text value to be entered, bjectName --> Name of the object
	 * Created by: BSS Automation team
	 * Created Date: 14 jan 2014
	 * Last Modified: 14 jan 2014
	 * */
	public static void enterText(String xpathObj, String textValue, String objectName ) throws IOException{

		if(driver.findElement(By.xpath(xpathObj)).isDisplayed()){
			driver.findElement(By.xpath(xpathObj)).clear();
			driver.findElement(By.xpath(xpathObj)).sendKeys(textValue);
			System.out.println("Pass: " + textValue + " is entered in a " + objectName + " Field.");
			Update_Report("Pass", "Enter text ", textValue + " is entered in a " + objectName + " Field.");	
			}
		else{
			System.out.println("Fail:" +objectName + " does not exist");
		}

	}

	/* Name of the Method: clickObject
	 * Method Description: Click on the object
	 * Arguments: xpathobj --> Xpath of the object,  bjectName --> Name of the object
	 * Created by: BSS Automation team
	 * Created Date: 14 jan 2014
	 * Last Modified: 14 jan 2014
	 * */
	public static void clickObject(String xPathObject,String objectName) throws IOException{
		if(driver.findElement(By.xpath(xPathObject)).isDisplayed()) {
			driver.findElement(By.xpath(xPathObject)).click();
			System.out.println("Pass:"+objectName+" has been clicked");
			Update_Report("Pass", "Click Object", objectName + " is Clicked");	
		}
		else {
			System.out.println("Fail: "+objectName+" is not displayed");
		}

	}

	/*Name of the Method: writeSheet
	 * Brief Description: writeSheet Data to Xl sheet
	 * Arguments: dataTablePath--> XL Sheet path, SheetName --> Name of the sheet to access, iRow --> Row number to enter data, iCol--> Column Number
	 * XLData --> Data needs to be filled
	 * Creation Date: Dec 19 2013
	 * Last updated: Dec 19 2013
	 * Created By: BetsSoft systems 
	 * */
	public static void writeSheet(String dataTablePath, String sheetName, int iRow, int iCol, String XlData) throws IOException{
		/* Access The Path of Xl File*/
		File xlFile = new File(dataTablePath );

		/* Access the File */
		FileInputStream xlDoc = new FileInputStream(xlFile);  

		/* Access The Work Book */
		HSSFWorkbook wb = new HSSFWorkbook(xlDoc);


		/* Access the Sheet */
		HSSFSheet sheet = wb.getSheet(sheetName);

		/* Access the Row*/
		HSSFRow row = sheet.getRow(iRow);

		/*Access the column*/
		HSSFCell cell = row.getCell(iCol);

		/* Set string Type */
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);

		/* set Value */
		cell.setCellValue(XlData);


		/* Set Color */
		HSSFCellStyle titleStyle = wb.createCellStyle();
		titleStyle.setFillForegroundColor(new HSSFColor.DARK_RED().getIndex());

		titleStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		cell.setCellStyle(titleStyle);

		FileOutputStream fout = new FileOutputStream(dataTablePath);
		wb.write(fout);
		fout.flush();
		fout.close();
	}
	/*Name of the Method: readData
	 * Brief Description: Reading Data from Xl sheet
	 * Arguments: dataTablePath--> XL Sheet path, SheetName --> Name of the sheet to access
	 * Creation Date: Dec 19 2013
	 * Last updated: Dec 19 2013
	 * Created By: BetsSoft systems 
	 * */

	/* Read data from data sheet */
	public static String[][] readData(String dataTablePath, String sheetName) throws IOException{
		String xlData[][] = null;

		/* Get the Path of the data Table*/

		File xlFile = new File(dataTablePath);
		


		/* Access the file*/
		FileInputStream xlDoc = new FileInputStream(xlFile);

		/*Access the work Book*/
		HSSFWorkbook wb = new HSSFWorkbook(xlDoc);

		/*Access the sheet*/
		HSSFSheet sheet = wb.getSheet(sheetName);

		int iRowCount = sheet.getLastRowNum()+1;
		int iColCount = sheet.getRow(0).getLastCellNum();

		/* Define length of string array*/
		xlData = new String[iRowCount][iColCount];

		for(int i = 0; i < iRowCount; i++){
			for(int j = 0; j < iColCount; j++){
				HSSFCell cell = sheet.getRow(i).getCell(j);
				xlData[i][j] = cell.getStringCellValue();
				//					System.out.print(cell.getStringCellValue() + " ");
			}
			//				System.out.println();

		}
		return xlData;
	}

	/**
	 * Method Name---> Chk_Obj
	 *  Description--->this method will select/deselect the object
	 * @param objXpath --> X path of the button, 2) buttonName --> Name of the Object
	 * Created By ----> Amandeep
	 * Creation date--> Jan 9th 2014
	 * @throws IOException 
	 */

	public static void Chk_Obj(String ObjName,String Name) throws IOException 
	{
		boolean chk;
		String a = ObjName;

		chk=driver.findElement(By.xpath(a)).isSelected();
		if((driver.findElement(By.xpath(a)).isDisplayed()))
		{
			if (chk==true)
			{
				System.out.println("No action performed already selected");
			}
			else 
			{
				driver.findElement(By.xpath(a)).click();
				System.out.println("Pass:"+a+" has been selected as "+Name);
				Update_Report("Pass", " Select Check box ", "Check box is selected");	

			}}
	}
	
	/* Method Name: validateEnteredText
	 * Brief Description: to validate Text Box is PRESENT or not AND is EMPTY or not
	 * Arguments used: ObjXpath --> xpath of text box field
	 * Created by: MUGDHA
	 * Created Date: Dec 8th 2013
	 * Last modified: Dec 8th 2013
	 */

	public static void validateEnteredText(String objXpath, String expectedValue, String objName) throws IOException{
		
		if(BaseClass.driver.findElement(By.xpath("//*[@id='username']")).isDisplayed()){
			String actulValue = BaseClass.driver.findElement(By.xpath("//*[@id='username']")).getAttribute("value");
			if(expectedValue.trim().equals(actulValue.trim())){
				System.out.println("Pass: Expected value '" + expectedValue + "' matches with the actual value");
				Update_Report("Pass", "Validate Entered Text ","Expected value '" + expectedValue + "' matches with the actual value" );
			}else{
				System.out.println("Fail: Expected value '" + expectedValue + "' did not match with actual value '"+ actulValue + "'.");
			}
				
			
		}else{
			System.out.println("Fail: Expected " + objName + " did not display in the application.");
		}
	}
		/* Method Name: LoginErrorMsgValidation
		 * Brief Description: to validate the error message
		 * Arguments used: xpathobj --> xpath of text box field, objectName
		 * Created by: Amandeep
		 * Created Date: Dec 16th 2013
		 * Last modified: Dec 16th 2013
		 */
		/* Arguments: xpathobj --> Xpath of the object,  objectName --> Name of the object*/
		
		public static void LoginErrorMsgValidation(String xpathobj,String objectName,String errMsg) throws IOException{
		
			String er=driver.findElement(By.xpath(xpathobj)).getText();
			System.out.println(objectName+" "+er);
			
			if(er.trim().equalsIgnoreCase(errMsg)){
				System.out.println("Pass: The expected error message was '"+errMsg+"' and actual was :"+er);
				Update_Report("Pass","Error message is displayed",er);
			}
			else{
				System.out.println("Fail:"+er+" is displayed");
			}
		}
			
		public static void ISShown(String xpathobj, String objectName)
			{
				if(driver.findElement(By.xpath(xpathobj)).isDisplayed()){
					System.out.println(objectName+"is displayed");
				//	Update_Report("Pass","is displayed"+objectName);
				}
				else
				{
					System.out.println(objectName+"is not displayed");
					//Update_Report("Fail"+objectName+"is not displayed");
				}
				}
	
		public static void verifyContent(String xpathobj, String objectName,String Objval)
		{
			if(driver.findElement(By.xpath(xpathobj)).isDisplayed()){
				String val=driver.findElement(By.xpath(xpathobj)).getText();
				if((val).equals(Objval)){
					System.out.println("Pass: "+objectName+"matches the saved  username");
				}
				else{
					System.out.println("Fail: "+objectName+" dont matches the saved  username");
				}
				
				}
			else{
				System.out.println("The"+objectName+" is not being displayed in the "+objectName+" Filed");
				
			}}
		
		public static void Clearfld(String xpathobj,String objname){
			
			if(driver.findElement(By.xpath(xpathobj)).isDisplayed()){
				driver.findElement(By.xpath(xpathobj)).clear();
			}
			else{
				System.out.println("Fail:"+objname+ " not availble on the page");
			}
		}
		
		}
		
				
			
		

	
	
	


